export class IndexService {
    constructor() { }
  
    verifyStep(activeIndex, formData) {
      let result = {};
      let rules = [];
      switch (activeIndex) {
        case 1:
          rules = [
            { rule: formData.supplier !== '', message: '请选择供应商' },
            { rule: formData.contract !== '', message: '请选择合同' }
          ];
          result = checkRule(rules);
          break;
        case 2:
          rules = [{ rule: formData.products.length > 0, message: '请添加产品' }];
          result = checkRule(rules);
          break;
        case 3:
          rules = [
            { rule: formData.carryway !== '', message: '请选择承运方式' },
            // { rule: formData.ordertype !== '', message: '请选择订单类型' },
            // { rule: formData.payway !== '', message: '请选择支付方式' }
          ];
          result = checkRule(rules);
          if (result.pass === false) {
            return result;
          }
  
          if (formData.carryway !== '') {
            rules = [{ rule: formData.address !== '', message: '请选择收货地址' }];
            result = checkRule(rules);
          }
          break;
        default:
          result.pass = true;
          break;
      }
      return result;
    }
  
    mappingSubmitData(submitData, formData) {
      let order_data = JSON.parse(JSON.stringify(submitData));
      order_data.page = 3;
      order_data.remark = formData.remark;
      order_data.transportType = formData.carryway;
      if (order_data.transportType === '1') {
        //qiyu 2020-4-3 自提的时候，在提交的时候清空收货地址 by 王康
        order_data.morderReceiverForms = {};
      } else {
        order_data.morderReceiverForms.addressPhone = formData.address_full.userPhone;
        order_data.morderReceiverForms.addressee = formData.address_full.userName;
        order_data.morderReceiverForms.area = formData.address_full.area;
        order_data.morderReceiverForms.city = formData.address_full.city;
        order_data.morderReceiverForms.province = formData.address_full.province;
        order_data.morderReceiverForms.detail = formData.address_full.detailedAddress;
      }
      return order_data;
    }
  
    mappingAppend2Extend(appendData) {
      let extend_data = [];
      appendData.map(item => {
        let extend_item = { type: 'input', id: item.orderAppendId, label: item.attributeName };
        extend_data.push(extend_item);
      });
      return extend_data;
    }
  
    mappingOrderDetail(orderDetail) {
      let form_data = {
        supplier: orderDetail?.sorderContractForm?.supplierCode,
        contract: orderDetail?.sorderContractForm?.contractCode,
        address: orderDetail?.morderReceiverForms?.id,
        extend: {},
        products: orderDetail?.purchaseProduct,
        id: orderDetail.orderId,
        detail_id: orderDetail.orderDetailId,
        remark: orderDetail.remark,
        carryway: String(orderDetail.transportType),
        operation: orderDetail.operationType
      };
      return form_data;
    }
    mappingOrderDetailReadonly(orderDetail) {
      let formdata = {
        supplier: orderDetail.orgName,
        contract: orderDetail.contract,
      };
      return formdata;
    }
  
    diffContract(formData, oldFormData) {
      if (formData.contract !== oldFormData.contract) {
        return true;
      } else {
        return false;
      }
    }
  
    resetOrder(formData) {
      let form_date = JSON.parse(JSON.stringify(formData));
  
      form_date.carryway = '';
      //...
      return form_date;
    }
  
    getFormContent(list, constObj) {
      let content = list.map(item => {
        let formatData = {};
        // 表单生成类型
        formatData.type = constObj[item.attributeType];
        formatData['el'] = {};
        formatData['el']['clearable'] = true
        formatData['el']['placeholder'] = item.attributeType === 1 || item.attributeType === 5 ? '请输入' : '请选择';
        if (item.attributeType === 6) {
          formatData['el']['type'] = 'datetime';
          formatData['el']['valueFormat'] = 'yyyy-MM-dd HH:mm:ss';
        } else if (item.attributeType === 4) {
          formatData['el']['valueFormat'] = 'yyyy-MM-dd';
        }
        formatData.id = item.attributeNameValue;
        formatData.label = item.attributeName;
        formatData.options = item.valueRange ? JSON.parse(item.valueRange) : [];
        formatData.rules = [];
        if (item.isMust) {
          formatData.rules.push({ required: true, message: '此项必填', trigger: 'change' })
        }
        if (item.attributeType === 5) {
          formatData.rules.push({ pattern: /^\d*$/, message: '请输入数字', trigger: 'blur' })
        }
        if (item.attributeType === 1 || item.attributeType === 5) {
          formatData.rules.push({ max: 50, message: '最长50个字符', trigger: 'blur' })
        }
        return formatData;
      });
      return content || [];
    }
  }
  
  /**
   * 检查规则，遇到rule=false的情况，立即扔出结果
   * @param {rule:bool, message:string} verifyRules
   */
  function checkRule(verifyRules) {
    let result = { pass: true, message: '' };
    for (let i = 0; i < verifyRules.length; i++) {
      const element = verifyRules[i];
      if (element.rule === true) {
        result.pass = true;
      } else {
        result.message = element.message;
        result.pass = false;
        return result;
      }
    }
    return result;
  }
  
  